import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import AppService from "../../../../common/AppService";
import moment from "moment";
import * as request from "../../../../services/CommonHttpService";

export default {
  namespace: 'bpData',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      if (!!payload.filters) {
        const time = !!payload.filters.time ? payload.filters.time : [];
        if (time.length === 2) {
          const startTime = !!time[0] && time[0].format('x');
          const endTime = !!time[1] && time[1].format('x');
          if (!!startTime && !!endTime)
            payload.filters = {...payload.filters, startTime, endTime};
        }

        delete payload.filters.time;
      }

      const p = {
        ...payload,
        server: AppService.dataUrl,
        baseUrl: getBaseUrl(),
        process: (records) => {
          return records.map(value => {
            return {
              ...value,
              time: (!!value.time) ? moment(value.time, 'x').format('YYYY-MM-DD HH:mm:ss') : ""
            };
          });
        }
      };

      yield put({
        type: 'baseGetPageInfo',
        payload: {...p}
      });
    },

    // 保存和修改
    * save({payload}, {put}) {
      payload.value.time = moment(payload.value.time, 'x').valueOf();
      yield put({
        type: 'baseSave',
        payload: {...payload, server: AppService.dataUrl, baseUrl: getBaseUrl()}
      });
    },


    // 删除 之后查询分页
    * delete({payload}, {call, put, select}) {
      console.log(payload);
      let bpInfos={bpInfos:payload.values};
      console.log(bpInfos);
      yield call(request.commonDelete, getBaseUrl(), {},bpInfos, AppService.dataUrl);
      payload.success();
    }
  }

}

function getBaseUrl() {
  return "/mlnx_data/bp/";
}

